package immutablecollections;

import java.util.*;

/**
 * An iterator on an {@link ImList}.
 * 
 * Because `ImLists` are immutable, {@link #remove} will throw {@link UnsupportedOperationException}
 *
 */
public class ImListIterator<T> implements Iterator<T>
{
    private ImList<T> list;

    public ImListIterator(ImList<T> list)
    {
        this.list = list;
    }

    public boolean hasNext()
    {
        return !list.isEmpty();
    }

    public T next()
    {
        if (hasNext())
        {
            T head = list.head();
            list = list.tail();
            return head;
        }
        else
        {
            throw new NoSuchElementException();
        }
    }

    /**
     * @deprecated Unsupported operation since `ImLists` are immutable.
     */
    public void remove()
    {
        throw new UnsupportedOperationException();
    }
}
